﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apworks;
using Apworks.Application;
using Apworks.Events;

namespace TinyForum.Infrastructure
{
    /// <summary>
    /// Represents the utility class.
    /// </summary>
    public static class CommonUtils
    {
        //private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("TinyForum.Logger");
        /// <summary>
        /// Checks whether the given <see cref="Apworks.IEntity"/> instance equals
        /// to the current <see cref="Apworks.IEntity"/> instance.
        /// </summary>
        /// <param name="this">The current instance.</param>
        /// <param name="other">The <see cref="Apworks.IEntity"/> instance to be checked.</param>
        /// <returns>True if the two are equal, otherwise, false.</returns>
        public static bool CheckEquality(this IEntity @this, IEntity other)
        {
            if (ReferenceEquals(@this, other))
                return true;
            if ((object)other == (object)null)
                return false;
            if (@this.GetType() != other.GetType())
                return false;
            return @this.ID == other.ID;
        }
        /// <summary>
        /// Gets the instance of <see cref="Apworks.ObjectContainer"/> class from the current Apworks application.
        /// </summary>
        public static ObjectContainer ObjectContainer
        {
            get { return AppRuntime.Instance.CurrentApplication.ObjectContainer; }
        }
        /// <summary>
        /// Publishes the domain event to the event bus.
        /// </summary>
        /// <param name="event">The event instance to be published.</param>
        public static void PublishEvent(IDomainEvent @event)
        {

        }
        /// <summary>
        /// Logs the specified message to the message logger.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void LogMessage(string message)
        {
            log.Info(message);
        }
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public static void LogException(string message, Exception ex)
        {
            log.Error(message, ex);
        }

        #region Extension Methods
        /// <summary>
        /// Encrypts the given plain text into a Base64 string.
        /// </summary>
        /// <param name="plainText">The plain text to be encrypted.</param>
        /// <returns>The encrypted Base64 string.</returns>
        //public static string EncryptBase64(this string plainText)
        //{
        //    byte[] bytes = Encoding.ASCII.GetBytes(plainText);
        //    return Convert.ToBase64String(bytes);
        //}
        /// <summary>
        /// Decrypts the given Base64 string into the plain text.
        /// </summary>
        /// <param name="base64String">The Base64 string to be decrypted.</param>
        /// <returns>The decrypted plain text.</returns>
        //public static string DecryptBase64(this string base64String)
        //{
        //    byte[] bytes = Convert.FromBase64String(base64String);
        //    return Encoding.ASCII.GetString(bytes);
        //}

        public static byte[] EncryptBase64(this byte[] source)
        {
            string base64String = Convert.ToBase64String(source);
            return Encoding.ASCII.GetBytes(base64String);
        }

        public static byte[] DecryptBase64(this byte[] source)
        {
            string base64String = Encoding.ASCII.GetString(source);
            return Convert.FromBase64String(base64String);
        }
        #endregion
    }
}
